package cn.dakaqi.activity.tag.services;

import cn.dakaqi.entities.ActivityTag;
import cn.dakaqi.entities.SysActivityTag;
import cn.dakaqi.sys.activity.tag.services.SysActivityTagService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/18.
 */
@Component(value = "activityTagService")
@Transactional
public class ActivityTagServiceImpl implements ActivityTagService
{
    @Autowired
    ActivityTagDao activityTagDao;
    @Autowired
    SysActivityTagService sysActivityTagService;

    @Override
    public void save(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");
        String[] name = jsonObject.getString("name").split(";");
        for(String str:name)
        {
            ActivityTag activityTag = new ActivityTag(memberCode,str);
            this.save(activityTag);
        }
    }

    @Override
    public void save(ActivityTag activityTag) throws ServiceRuntimeException
    {
        this.activityTagDao.save(activityTag);
    }

    @Override
    public ActivityTag findOne(Long id) throws ServiceRuntimeException
    {
        return this.activityTagDao.findOne(id);
    }

    @Override
    public List<SysActivityTag> findSysTag() throws ServiceRuntimeException
    {
        return this.sysActivityTagService.findAll();
    }

    @Override
    public Page<ActivityTag> findByMemberCode(String memberCode, String name, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String,Object> map = new HashMap<String, Object>();
        //map.put("LIKE_NAME",name);
        //Map<String, SearchFilter> filters = SearchFilter.parse(map);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("memberCode", new SearchFilter("memberCode", SearchFilter.Operator.EQ, memberCode));
        if(StringUtils.isNotBlank(name))
            filters.put("name", new SearchFilter("name", SearchFilter.Operator.LIKE, name));
        Specification<ActivityTag> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityTag.class);
        return this.activityTagDao.findAll(spec, pageRequest);
    }
}
